#include "TextureLoader.h"

GLuint TextureLoader::loadTexture( const std::string& filename )
{
	SDL_Surface* surf = SDL_LoadBMP(filename.c_str());
	if (!surf) return 0;

	GLint colors;
	GLenum format;

	colors = surf->format->BytesPerPixel;

	if (colors == 4)
	{
		if (surf->format->Rmask == 0x000000ff) format = GL_RGBA;
		else format = GL_BGRA;
	} else {
		if (surf->format->Rmask == 0x000000ff) format = GL_RGB;
		else format = GL_BGR;
	}

	GLuint tex;
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &tex);

	glBindTexture(GL_TEXTURE_2D, tex);

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexImage2D(GL_TEXTURE_2D, 0, colors, surf->w, surf->h, 0, format, GL_UNSIGNED_BYTE, surf->pixels );

	SDL_FreeSurface(surf);

	return tex;
}